package cn.hotel.servlet;

import cn.hotel.pojo.Member;
import cn.hotel.service.MemberService;
import cn.hotel.service.MemberServiceImpl;
import cn.hotel.utlis.PageBean;
import cn.hotel.utlis.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

@WebServlet(value = {"/memberwork"})
public class MemberServlet extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=utf-8");
        MemberService memberService = new MemberServiceImpl();
        String type = req.getParameter("type");
        type = StringUtil.isEmpty(type) ? "list" : type;
        switch (type){
            case "list":
                getMemberList(memberService,req,resp);
                break;
            case "delete":
                    //删除
                    memberDelete(req,resp,memberService,out);
                    break;

        }
    }

    private void memberDelete(HttpServletRequest req, HttpServletResponse resp, MemberService memberService, PrintWriter out) {

        String deleteMember = req.getParameter("account");
        String appPath = req.getContextPath();
        Map<String ,Object> deleteMap = memberService.deleteMember(deleteMember);
        if("1".equals(deleteMap.get("flag"))){
            out.print("<script>alert('删除成功');location.href='"+appPath+"/memberwork?type=list'</script>");
        }else {
            out.print("<script>alert('删除失败');location.href='"+appPath+"/memberwork?type=list'</script>");
        }
        out.flush();

    }

    private void getMemberList(MemberService memberService, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageBean<Member> pageBean = new PageBean<>();
        //接收account用来查询
        String account = req.getParameter("account");

        //account = StringUtil.isEmpty(account) ? "" : account;
        String current = req.getParameter("current");
        int currentIndex = StringUtil.strToInt(current);
        //-计算总条数
        int count = memberService.getCount(account);
        //将总条数放到pageBean中
        pageBean.setTotalCount(count);
        //设置当前页
        pageBean.setCurrPageNo(currentIndex);
        //下面第一行有点小问题第二个参数
        List<Member> memberList = memberService.getMemberList(account,pageBean.getCurrPageNo(),pageBean.getPageSize());

        pageBean.setProductList(memberList);
        req.setAttribute("pageBean",pageBean);
        req.getRequestDispatcher("memberjsp/list.jsp").forward(req,resp);
    }
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
